package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Comment;
import com.softeem.music_sys.service.CommentService;
import com.softeem.music_sys.mapper.CommentMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author RD
* @description 针对表【comment(评论信息表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{

    @Override
    public Page<Comment> getCommentsByTarget(Object type, Integer targetId, Integer page, Integer limit) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("target_id", targetId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("comment_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Comment> getCommentsByUser(Integer userId, Integer page, Integer limit) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("comment_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public List<Comment> getHotComments(Integer limit) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("like_count");
        wrapper.last("limit " + limit);
        return list(wrapper);
    }

    @Override
    public List<Comment> getNewComments(Integer limit) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("comment_time");
        wrapper.last("limit " + limit);
        return list(wrapper);
    }

    @Override
    public boolean likeComment(Integer commentId) {
        Comment comment = getById(commentId);
        if (comment != null && comment.getStatus() == 1) {
            comment.setLikeCount(comment.getLikeCount() + 1);
            return updateById(comment);
        }
        return false;
    }
    
    @Override
    public Result addComment(Comment comment) {
        // 设置评论时间、状态和点赞数
        comment.setCommentTime(new Date());
        comment.setStatus(1);
        if (comment.getLikeCount() == null) {
            comment.setLikeCount(0);
        }
        
        boolean result = this.save(comment);
        return result ? Result.success().setMessage("评论成功") : Result.fail("评论失败");
    }
    
    @Override
    public Result deleteComment(Integer id) {
        Comment comment = this.getById(id);
        if (comment == null) {
            return Result.fail("评论不存在");
        }
        // 软删除：修改状态为0
        comment.setStatus(0);
        boolean result = this.updateById(comment);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }
    
    @Override
    public Result getCommentById(Integer id) {
        Comment comment = this.getById(id);
        if (comment == null) {
            return Result.fail("评论不存在");
        }
        return Result.success().setData(comment);
    }
    
    @Override
    public Result getCommentsByTargetController(Object type, Integer targetId, Integer page, Integer limit) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("target_id", targetId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("comment_time");
        Page<Comment> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getCommentsByUserController(Integer userId, Integer page, Integer limit) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("comment_time");
        Page<Comment> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result likeCommentController(Integer id) {
        Comment comment = this.getById(id);
        if (comment == null) {
            return Result.fail("评论不存在");
        }
        if (comment.getStatus() == 0) {
            return Result.fail("评论已删除");
        }
        
        // 点赞数+1
        comment.setLikeCount(comment.getLikeCount() + 1);
        boolean result = this.updateById(comment);
        return result ? Result.success().setMessage("点赞成功").setData(comment) : Result.fail("点赞失败");
    }
    
    @Override
    public Result getHotCommentsController(Integer page, Integer limit) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("like_count");
        Page<Comment> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getNewCommentsController(Integer page, Integer limit) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("comment_time");
        Page<Comment> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getAllCommentsController(Integer page, Integer limit) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        Page<Comment> resultPage = this.page(pageUtil);
        return Result.success().setData(resultPage);
    }
}